﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Home.MobileSales.Common.Components;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.Sales.Interface;
using Home.MobileSales.Sales.Presenter;

namespace Home.MobileSales.Sales.View
{
    public partial class DailyVisitForm : Form, IDailyVisitsView
    {
        #region IDailyVisitsForm Members

        public object DataSource
        {
            set {
                dailyVisitsDataGrid.DataSource = value;                 
            }
            get { return dailyVisitsDataGrid.DataSource; }
        }

        public object Current
        {
            get { return dailyVisitsDataGrid.CurrentRowIndex; }
        }

        public ControlCollection ParentControl {
            get
            {
                return addBuyPointPanel.Controls;
            }

        }

        public void ShowAddVisitDialog(AddVisitDialog visitDialog)
        {
            visitDialog.Dock = DockStyle.Fill;
            addBuyPointPanel.Controls.Add(visitDialog);
            //mainPanel.Enabled = false;
            addBuyPointPanel.Visible = true;
            //addBuyPointPanel.Enabled = true;
        }

        public void CloseAddVisitDialog()
        {
            addBuyPointPanel.Visible = false;
        }

        public event EventHandler<EventArgs> AddVisit;

        public event EventHandler<EventArgs> DeleteVisit;

        public event EventHandler<EventArgs> CreateFromShedule;

        #endregion

        public DailyVisitForm(DateTime dateTime)
        {
            InitializeComponent();
            new DailyVisitsPresenter(this, dateTime);
            
            DataGridTextBoxColumn dataColumn;
            DataGridTableStyle ts = new DataGridTableStyle();

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Назва торгової точки";
            dataColumn.MappingName = "ByPointName";
            dataColumn.NullText = "";
            dataColumn.Width = 150;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Адреса";
            dataColumn.MappingName = "BuyPointAdress";
            dataColumn.NullText = "";
            dataColumn.Width = 200;
            ts.GridColumnStyles.Add(dataColumn);
            ts.MappingName = dailyVisitsDataGrid.DataSource.GetType().Name;
            dailyVisitsDataGrid.TableStyles.Add(ts);

            dailyVisitsDataGrid.RowHeadersVisible = false;
            deleteMenuItem.Enabled = false;
            Cursor.Current = Cursors.Default;
            
        }
        
        private void loadMenuItem_Click(object sender, EventArgs e)
        {

                CreateFromShedule(null, null);

        }

        private void addMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            AddVisit(null, null);
        }

        private void deleteMenuItem_Click(object sender, EventArgs e)
        {
                DeleteVisit(null, null);
        }

        private void dailyVisitsDataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
            deleteMenuItem.Enabled = true;
            if (dailyVisitsDataGrid.CurrentRowIndex < 0 || ((List<Visit>)dailyVisitsDataGrid.DataSource)[dailyVisitsDataGrid.CurrentRowIndex].IsReadOnly)
                deleteMenuItem.Enabled = false;
            
        }

    }
}